#include "playbarview.h"
#include "ui_playbarview.h"
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QUrl>
#include <QTimer>
#include <QLabel>
#include "playEngine/ezvoiceplayer.h"
#include "playEngine/ezvoiceplayermanager.h"
#include "model/playListModel/playlistbasemodel.h"
#include "model/commonModel/songmodel.h"
#include "playListView/nowplaylistview.h"
#include <QMenu>
#include "mainwindow.h"
#include "log/EZQLOG.h"
#include <QMouseEvent>
#include "mainwindow.h"
#include "imageBll/loadimagebll.h"

PlayBarView::PlayBarView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlayBarView)
{
    ui->setupUi(this);
//    http://www.5ydj.com/upmusic/16577/3C926417E2D7C732B68D5DE5E1BF51A9.mp3

    EZQAudioPlayer *audioPlayer = EZQAudioPlayer::sharePlayer();
    connect(audioPlayer,SIGNAL(sigStausChanged(EZQAudioPlayStatus)),this,SLOT(onAudioStatusChange(EZQAudioPlayStatus)));

    EZVoicePlayerManager *playManager = EZVoicePlayerManager::defaultManager();
    connect(playManager,SIGNAL(sigPlayIndexChange(int)),this,SLOT(onPlayIndexChange(int)));
    connect(playManager,SIGNAL(sigPlayQueueTypeChange()),this,SLOT(onPlayQueueTypeChange()));
    connect(playManager,SIGNAL(sigPlayListContentChange()),this,SLOT(onPlayContentChange()));

    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(onTimeOut()));
    timer->start(1000);

    connect(ui->playTime,SIGNAL(sliderReleased()),this,SLOT(onSliderValueChange()));
    ui->playTime->setTracking(false);

    ui->sliderVolume->setMaximum(100);
    ui->sliderVolume->setValue(audioPlayer->volume());
//    connect(ui->sliderVolume,SIGNAL(valueChanged(int)),player,SLOT(setVolume(int)));

    connect(ui->playType,SIGNAL(clicked()),this,SLOT(onPlayQueueTypeBtClick()));

    _playListView = new NowPlayListView(this);
    connect(ui->playList,SIGNAL(clicked()),this,SLOT(onShowPlayListClick()));
}

PlayBarView::~PlayBarView()
{
    delete ui;
}

void PlayBarView::playEvent()
{
//    QApplication::postEvent(&bll);

    EZVoicePlayerManager::defaultManager()->playOrPause();
//    PlayListBaseModel *playList = EZVoicePlayerManager::defaultManager()->getPlayList();
//    static int a = 0;
//    a++;
//    int b = a%playList->count();
//    EZVoicePlayerManager::defaultManager()->playIndex(b);

}
void PlayBarView::playNextEvent()
{
    EZVoicePlayerManager::defaultManager()->playNext();
}

void PlayBarView::playPreviousEvent()
{
    EZVoicePlayerManager::defaultManager()->playPrevious();
}

void PlayBarView::playTestError(QMediaPlayer::Error error)
{
    qDebug("player error:%d",error);
}

void PlayBarView::stateChanged(EZVoiceState state)
{

    if(state.playState == EZVoicePlayerState_plyaing){
        ui->btPlay->setChecked(true);
    }else{
        ui->btPlay->setChecked(false);
    }
    qDebug() << "player stateChanged" << state.playState << ":" << state.mediaState << ":" <<state.subMediaState;
}
void PlayBarView::onAudioStatusChange(EZQAudioPlayStatus status)
{
    EZQAudioPlayer *player = EZQAudioPlayer::sharePlayer();
    bool willPlayer = player->willPlay();
    if(willPlayer){
        ui->btPlay->setChecked(true);
    }else{
        ui->btPlay->setChecked(false);
    }
    if(status == EZQAudioPlayStatus_playing){
        //already in playing
        ui->btPlay->setChecked(true);
    }else if(status == EZQAudioPlayStatus_init){
        //cant not play
    }else if(status == EZQAudioPlayStatus_preStart){
        // in decode probe
    }else if(status == EZQAudioPlayStatus_preIng){

    }else if(status == EZQAudioPlayStatus_preEnd){

    }else if(status == EZQAudioPlayStatus_bufStart){

    }else if(status == EZQAudioPlayStatus_bufIng){

    }else if(status == EZQAudioPlayStatus_bufEnd){
        //buff is enough go real play
    }else if(status == EZQAudioPlayStatus_pause){
        // on pause
        ui->btPlay->setChecked(false);
    }else if(status == EZQAudioPlayStatus_stop){
        // play is stop so  undecode  start decode first
        ui->btPlay->setChecked(false);
    }else if(status == EZQAudioPlayStatus_finish){
        // play is stop so  undecode  start decode first
        ui->btPlay->setChecked(false);
    }else if(status == EZQAudioPlayStatus_error){
        // play is erro start decode again
        ui->btPlay->setChecked(false);
    }
}

void PlayBarView::onPlayIndexChange(int index)
{
    EZVoicePlayerManager *manager = (EZVoicePlayerManager*)sender();
    SongModel *song = manager->getPlayList()->songAtIndex(index);
    if(song){
//        ui->songHead->loadImageWithUrl(song->)
        ui->songTitle->setText(song->title());
    }else{
        ui->songTitle->setText(QString(""));
    }
    ui->playTime->setMaximum(0);
    ui->playTime->setValue(0);
    ui->playCurrentTime->setText("00:00");
    ui->playDuration->setText("00:00");
}
void PlayBarView::onPlayQueueTypeChange()
{
    EZVoicePlayerManager *manager = EZVoicePlayerManager::defaultManager();
    ui->playType->setQueueType(manager->getPlayList()->getPlayQueueType());
}

void PlayBarView::onPlayContentChange()
{

    this->onPlayQueueTypeChange();
}

void PlayBarView::onTimeOut()
{
//    if(ui->playTime->isSliderDown()==true){
//        return;
//    }
//    EZVoicePlayer *player = EZVoicePlayer::defaultPlayer();
    EZQAudioPlayer *audioPlayr = EZQAudioPlayer::sharePlayer();

    int duration = audioPlayr->duration()/1000000;
    int curTime = audioPlayr->currentTime()/1000000;

    ui->playTime->setMaximum(duration);
    ui->playTime->setValue(curTime);

    QString currentTime;

    if(curTime > 60 * 60){
        char ctime[100];
        sprintf(ctime,"%d:%.2d:%.2d",curTime/3600,(curTime/3600)/60,curTime%60);
        currentTime = QString(ctime);
    }else{
        char ctime[100];
        sprintf(ctime,"%.2d:%.2d",curTime/60,curTime%60);
        currentTime = QString(ctime);
    }
    ui->playCurrentTime->setText(currentTime);

    QString durationTime;

    if(duration> 3600){
        char ctime[100];
        sprintf(ctime,"%d:%.2d:%.2d",duration/3600,(duration/3600)/60,duration%60);
        durationTime = QString(ctime);
    }else{
        char ctime[100];
        sprintf(ctime,"%.2d:%.2d",duration/60,duration%60);
        durationTime = QString(ctime);
    }
    ui->playDuration->setText(durationTime);
//    qDebug() << player->currentTime();
}

void PlayBarView::onSliderValueChange()
{
    qDebug() << "onSliderValueChange:" << ui->playTime->sliderPosition();
//    EZVoicePlayer *player = EZVoicePlayer::defaultPlayer();
//    player->setCurrentTime(ui->playTime->sliderPosition());
    EZQAudioPlayer *audioPlayer = EZQAudioPlayer::sharePlayer();
    audioPlayer->setCurrentTime(ui->playTime->sliderPosition()*1000000);
}

void PlayBarView::onPlayQueueTypeBtClick()
{
    PlayListBaseModel::QUEUE_TYPE newType = (PlayListBaseModel::QUEUE_TYPE)((ui->playType->queueType() +1)%3);
    EZVoicePlayerManager::defaultManager()->getPlayList()->setPlayQueueType(newType);

}

void PlayBarView::onShowPlayListClick()
{

    QPoint point = ui->playList->mapToGlobal(QPoint(ui->playList->width()/2,ui->playList->height()));
    point.setX(point.x() - _playListView->width()/2);
    _playListView->move(point);
    _playListView->show();

}
void PlayBarView::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
//    if(_playListView->isHidden()==false){
//         _playListView->setGeometry(0,0,_playListView->parentWidget()->width(),_playListView->parentWidget()->height());
//         QPoint point = ui->playList->mapTo(_playListView->parentWidget(),QPoint(ui->playList->width()/2,ui->playList->height()+5));
//         _playListView->setFitPoint(point);
//    }
}

void PlayBarView::mousePressEvent(QMouseEvent * event)
{
    _upPoint = event->globalPos();
    QWidget::mousePressEvent(event);
}

void PlayBarView::mouseReleaseEvent(QMouseEvent * event)
{
    _upPoint = QPoint(-1,-1);
    QWidget::mouseReleaseEvent(event);
}

void PlayBarView::mouseMoveEvent(QMouseEvent * event)
{
    if(_upPoint.x() < 0){
        return;
    }
    QPoint nowPoint = event->globalPos();
    MainWindow *m = MainWindow::shareWindow();
    m->move(m->pos()+(nowPoint-_upPoint));
    _upPoint = nowPoint;

    QWidget::mouseMoveEvent(event);
//    EZDebug << event->type() << event->globalPos() ;
}
